package com.project.auth.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.auth.mapper.ProcessTemplateMapper;
import com.project.auth.model.process.ProcessTemplate;
import com.project.auth.model.process.ProcessType;
import com.project.auth.service.ProcessService;
import com.project.auth.service.ProcessTemplateService;
import com.project.auth.service.ProcessTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author : XIAOMING
 * @create 2023/5/31 16:09
 */
@Service
public class ProcessTemplateServiceImpl extends ServiceImpl<ProcessTemplateMapper, ProcessTemplate> implements ProcessTemplateService {

    @Autowired
    private ProcessTypeService processTypeService;
    @Autowired
    private ProcessService processService;

    /**
     * 分页查询
     *
     * @param pageParam
     * @return
     */
    @Override
    public IPage<ProcessTemplate> selectPageProcessTemplate(Page<ProcessTemplate> pageParam) {
        IPage<ProcessTemplate> page = this.page(pageParam);
        List<ProcessTemplate> records = page.getRecords().stream().map(record -> {
            ProcessType processType = processTypeService.getById(record.getProcessTypeId());
            if (processType == null) {
                return record;
            }
            String typeName = processType.getName();
            record.setProcessTypeName(typeName);

            return record;
        }).collect(Collectors.toList());
        page.setRecords(records);

        return page;
    }

    @Override
    public void publish(Long id) {
        ProcessTemplate processTemplate = baseMapper.selectById(id);
        processTemplate.setStatus(1);//1为已发布
        baseMapper.updateById(processTemplate);

        //部署
        if (StringUtils.isNotEmpty(processTemplate.getProcessDefinitionPath())) {
            processService.deployByZip(processTemplate.getProcessDefinitionPath());
        }

    }
}
